const path = require('path');
const autoprefixer = require("autoprefixer");
const webpack = require('webpack');
const UglifyJsPlugin = webpack.optimize.UglifyJsPlugin;
const env = require('yargs').argv.env; // use --env with webpack 2
const fs = require('fs');
const VueLoaderPlugin = require('vue-loader/lib/plugin');

let libraryName = 'EasyDesigner';

let plugins = [new VueLoaderPlugin()];
let entry = {};
let outputFile;
entry[libraryName] = './src/index.js';
let filenames = fs.readdirSync('./src/components/');
for (let filename of filenames) {
    if (filename.endsWith('.vue')) {
        entry[filename.slice(0, -4)] = './src/components/' + filename;
    }
}

if (env === 'build') {
    plugins.push(new UglifyJsPlugin({ minimize: true }));
    outputFile = '[name].min.js';
} else {
    outputFile = '[name].js';
}

module.exports = {
    entry,
    devtool: 'source-map',
    // mode: env === 'build' ? 'production' : 'development',
    output: {
        path: path.join(__dirname, 'dist'),
        filename: outputFile,
        library: '[name]',
        libraryTarget: 'umd',
        umdNamedDefine: true
    },
    module: {
        strictExportPresence: true,
        noParse: [/moment.js/],
        rules: [{
                test: /\.(js|jsx)$/,
                include: path.join(__dirname, "src"),
                loader: 'babel-loader',
                options: {
                    cacheDirectory: true,
                    presets: ["latest"]
                },
            },
            {
                test: /\.vue$/,
                loader: 'vue-loader'
            },
            {
                test: /\.css$/,
                use: [
                    "style-loader",
                    // 'vue-style-loader',
                    'css-loader',
                    {
                        loader: 'postcss-loader',
                        options: {
                            ident: 'postcss',
                            plugins: () => [
                                require('postcss-flexbugs-fixes'),
                                autoprefixer({
                                    browsers: ['>1%', 'last 4 versions', 'Firefox ESR', 'not ie < 9'],
                                    flexbox: 'no-2009',
                                }),
                            ],
                        },
                    },
                ],
            },
            {
                test: /\.less$/,
                use: [
                    "style-loader",
                    // 'vue-style-loader',
                    'css-loader',
                    {
                        loader: 'postcss-loader',
                        options: {
                            ident: 'postcss',
                            plugins: () => [
                                require('postcss-flexbugs-fixes'),
                                autoprefixer({
                                    browsers: ['>1%', 'last 4 versions', 'Firefox ESR', 'not ie < 9'],
                                    flexbox: 'no-2009',
                                }),
                            ],
                        },
                    },
                    'less-loader'
                ]
            },
            {
                test: /\.(bmp|png|jpe?g|gif)$/i,
                loader: 'url-loader',
                options: {
                    limit: 10000,
                    name: 'static/media/[name].[hash:8].[ext]',
                },
            },
            {
                test: /\.(bmp|png|jpe?g|gif)$/i,
                loader: 'file-loader',
                options: {
                    name: 'static/media/[name].[hash:8].[ext]',
                },
            }
        ],
    },
    plugins: plugins
};